package com.qiyun.duijiang.service.imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qiyun.duijiang.dao.ForderMapper;
import com.qiyun.duijiang.dao.JczqResultMapper;
import com.qiyun.duijiang.dao.RecordMapper;
import com.qiyun.duijiang.dao.UserMapper;
import com.qiyun.duijiang.entity.*;
import com.qiyun.duijiang.service.FprizeService;
import com.qiyun.duijiang.utils.Futil;
import com.qiyun.duijiang.utils.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class FprizeServiceImp implements FprizeService {

    @Resource
    private ForderMapper forderMapper;
    @Resource
    private JczqResultMapper jczqResultMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RecordMapper recordMapper;

    @Override
    public void draw() {
        List<Forder> forders = forderMapper.selectT(TimeUtil.time2());
        for (Forder forder:forders){
            Double authup = getPoints(forder);
            win(authup,forder);
        }
    }

    /**
     * 用户加点，修改订单状态
     * @param authup
     * @param forder
     */
    @Transactional
    protected void win(Double authup,Forder forder){
        if(StringUtils.isEmpty(authup)){
            forder.setStatus(3);
        }else{
            User user = new User();
            user.setUsername(forder.getUsername());
            user.setAuth(authup);
            if(authup>0) {
                Record record = new Record();
                record.setCdate(TimeUtil.dayX());
                record.setWay(4);
                record.setAuth(authup);
                record.setUsername(forder.getUsername());
                recordMapper.insert(record);
            }
            userMapper.updateA(user);
            forder.setStatus(1);
            forder.setWinauth(authup);
        }
        forderMapper.updateS(forder);

    }

    /**
     * 计算方案中奖点数
     * @param forder
     * @return
     */
    public Double getPoints(Forder forder){
        List<ForderVo1> forderVo1s = JSON.parseArray(forder.getResult(),ForderVo1.class);
        if (null==forderVo1s||forderVo1s.isEmpty()){
            return 0.00;
        }
        Map<Integer,Double> map = new HashMap<>();
        for (ForderVo1 forderVo1 : forderVo1s) {
            Integer matchid = forderVo1.getMatchid();
            JczqResult jczqResult = jczqResultMapper.selectById(matchid);
            if(StringUtils.isEmpty(jczqResult)){
                return null;
            }
            String spf[] = jczqResult.getSpf().split(",");
            String rq[] = jczqResult.getRq().split(",");
            String bf[] = jczqResult.getBf().split(",");
            String jq[] = jczqResult.getJq().split(",");
            String bqc[] = jczqResult.getBqc().split(",");

            String pass = forderVo1.getType();
            Double odds;
            if(pass.equals("spf")){
                odds = odds(forderVo1,spf[0]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }else if(pass.equals("rq")){
                odds = odds(forderVo1,rq[1]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }else if(pass.equals("bf")){
                odds = odds(forderVo1,bf[0]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }else if(pass.equals("jq")){
                odds = odds(forderVo1,jq[0]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }else if(pass.equals("bqc")){
                odds = odds(forderVo1,bqc[0]+bqc[1]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }
        }if(map.size()<=0){
            return 0.00;
        };
        Double authup = Futil.authUp(map,forder.getStrand());
        return authup*forder.getNum();
    }
    public Double odds(ForderVo1 forderVo1, String s){
        boolean r = forderVo1.getChooses().contains(s);
        if (r){
            String[] arr = forderVo1.getChooses().split(",");
            String[] brr = forderVo1.getOddses().split(",");
            for (int i=0;i<arr.length;i++){
                if(s.equals(arr[i])){
                    return Double.valueOf(brr[i]);
                }
            }
        }
        return null;
    }
}
